﻿//新闻图片切换
var container;
var links;
var controlContainer;
var controlers;
var picInfo;
var timeInterval = 3000;
var length;
var curFlag;
var defaultStyle = "default";
var selectedStyle = "selected";

function innerSwitch(id) {
    var containerId = "#" + id;
    container = $(containerId);

    controlContainer = $(container.children("div")[0]);
    picInfo = $(container.children("span")[0]);
    links = container.children("a");
    length = links.length;

    $(links[0]).children("img").animate({ opacity: 'show' }, "slow");
    $(picInfo).html($(links[0]).children("img").attr("alt"));
    curFlag = 0;

    for (var m = 0; m < length; m++) {
        var controler = $("<div></div>");
        controler.html((m + 1) + "");
        controler.attr("rel", m);
        controler.attr("class", defaultStyle);

        controler.mouseover(
					function() {
					    curFlag = $(this).attr("rel");
					    setStyle(curFlag);
					}
				)

        controlContainer.append(controler);
        controlers = controlContainer.children("div");
    }
}

function setStyle(flag) {
    $(picInfo).html($(links[flag]).children("img").attr("alt"));

    for (var i = 0; i < length; i++) {

        if (flag == i) {
            $(links[i]).children("img").animate({ opacity: 'show' }, "slow");
            $(controlers[i]).attr("class", selectedStyle);
            continue;
        }

        $(links[i]).children("img").hide();
        $(controlers[i]).attr("class", defaultStyle);
    }
}

function autoSwitch() {
    if (curFlag >= length) {
        curFlag = 0;
    }

    setStyle(curFlag);

    curFlag++;
    setTimeout("autoSwitch()", timeInterval);
}